package com.fenghongzhang.day010;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MyBottomNavigationBar extends FrameLayout implements View.OnClickListener {

    private static final String TAG = "MyBottomNavigationBar";
    private int backColor;
    private LinearLayout linearLayout;

    private LinearLayout.LayoutParams layoutParams;
    private List<MyBottomNavigationItem> myBottomNavigationItemList;
    public MyBottomNavigationBar(@NonNull Context context) {
        super(context);
        init();
    }

    public MyBottomNavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public void setBackColor(int backColor) {
        this.backColor = backColor;
    }

    private void init() {
        Log.i(TAG, "init: ");
        //宽高
        setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));

        myBottomNavigationItemList = new ArrayList<>();
        //

        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.bottom_layout, this,true);
        backColor = Color.BLACK;
        inflate.setBackgroundColor(backColor);
        FrameLayout frameLayout = inflate.findViewById(R.id.bottom_container);
        frameLayout.setBackgroundColor(backColor);

        linearLayout = inflate.findViewById(R.id.ll_container);

        //创建布局参数
         layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
         layoutParams.gravity = Gravity.CENTER;
         layoutParams.weight = 1;
         linearLayout.setGravity(Gravity.CENTER);
    }

    public MyBottomNavigationBar addItem(MyBottomNavigationItem item){
        myBottomNavigationItemList.add(item);
        return this;
    }

    public void build(){
        int size = myBottomNavigationItemList.size();
        for (int i = 0; i < size; i++) {
            MyBottomNavigationItem myBottomNavigationItem = myBottomNavigationItemList.get(i);
            String title = myBottomNavigationItem.getTitle();
            if(title != null){
                TextView textView = new TextView(getContext());
                textView.setText(title);
                textView.setTextColor(Color.GRAY);
                textView.setLayoutParams(layoutParams);
                textView.setGravity(Gravity.CENTER);
                textView.setTag(R.id.bottom_id,i);
                textView.setOnClickListener(this);
                linearLayout.addView(textView);
            }
            Drawable drawable = myBottomNavigationItem.getDrawable();
            if(drawable !=null){
                ImageView imageView = new ImageView(getContext());
                imageView.setImageDrawable(drawable);
                imageView.setTag(R.id.bottom_id,i);
                imageView.setOnClickListener(this);
                linearLayout.addView(imageView);
            }
        }
    }

    @Override
    public void onClick(View v) {
        //判断是点击了谁
        int i = (int) v.getTag(R.id.bottom_id);
        Log.i(TAG, "onClick: "+i);
        myListener.selectItem(i);
    }


    public interface MyListener{
        void selectItem(int i);
    }
    private MyListener myListener;

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }
}
